package org.dromara.bykt.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bykt.domain.bo.BYKTGeographyInfoBo;
import org.dromara.bykt.domain.vo.BYKTGeographyInfoVo;
import org.dromara.bykt.domain.BYKTGeographyInfo;
import org.dromara.bykt.mapper.BYKTGeographyInfoMapper;
import org.dromara.bykt.service.IBYKTGeographyInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 省份Service业务层处理
 *
 * @author Hou
 * @date 2025-07-02
 */
@RequiredArgsConstructor
@Service
public class BYKTGeographyInfoServiceImpl implements IBYKTGeographyInfoService {

    private final BYKTGeographyInfoMapper baseMapper;

    /**
     * 查询省份
     *
     * @param id 主键
     * @return 省份
     */
    @Override
    public BYKTGeographyInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询省份列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 省份分页列表
     */
    @Override
    public TableDataInfo<BYKTGeographyInfoVo> queryPageList(BYKTGeographyInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BYKTGeographyInfo> lqw = buildQueryWrapper(bo);
        Page<BYKTGeographyInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的省份列表
     *
     * @param bo 查询条件
     * @return 省份列表
     */
    @Override
    public List<BYKTGeographyInfoVo> queryList(BYKTGeographyInfoBo bo) {
        LambdaQueryWrapper<BYKTGeographyInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BYKTGeographyInfo> buildQueryWrapper(BYKTGeographyInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BYKTGeographyInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BYKTGeographyInfo::getId);
        lqw.like(StringUtils.isNotBlank(bo.getName()), BYKTGeographyInfo::getName, bo.getName());
        lqw.eq(bo.getStatus() != null, BYKTGeographyInfo::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增省份
     *
     * @param bo 省份
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BYKTGeographyInfoBo bo) {
        BYKTGeographyInfo add = MapstructUtils.convert(bo, BYKTGeographyInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改省份
     *
     * @param bo 省份
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BYKTGeographyInfoBo bo) {
        BYKTGeographyInfo update = MapstructUtils.convert(bo, BYKTGeographyInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BYKTGeographyInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除省份信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 查询省份下拉
     *
     * @return
     */
    @Override
    public List<BYKTGeographyInfoVo> queryListAll() {
        return baseMapper.selectVoList(null);
    }

    @Override
    public Long selectName(String name) {
        BYKTGeographyInfoVo byktGeographyInfoVo = baseMapper.selectVoOne(new LambdaQueryWrapper<BYKTGeographyInfo>().eq(BYKTGeographyInfo::getName, name));
        if(byktGeographyInfoVo != null){
            return byktGeographyInfoVo.getId();
        }
        return 0L;
    }
}
